package com.syphernova.learning.amst3;

import java.util.concurrent.ExecutionException;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayXthJSONhttp extends ActionBarActivity {

	private static final String TAG = "JSON_AndroidHTTPClient";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display_xth_jsonhttp);
		
		Intent receiver = getIntent();
		int xvalue = receiver.getIntExtra(InputxJSON.EXTRA_XVALUE, 0);
		
		TextView tw = (TextView)findViewById(R.id.jsonoutputhttp);
		tw.setText("Fetching Data...please wait!");
		
		String hostName = "earthquake.usgs.gov";
		String pageName = "/earthquakes/feed/geojsonp/2.5/week";
		String xvaluri = String.valueOf(xvalue);
		String hostURL = hostName;
		
		RetrieveJSON jsonFetcher = new RetrieveJSON(getApplicationContext());
		
		String XthLine = "The string is";
		
		try {
			XthLine = jsonFetcher.execute(xvaluri, hostURL, pageName).get();
		} catch (InterruptedException e) {
			Log.d(TAG, "Interrupted exception in display through Socket!");
		} catch (ExecutionException e) {
			Log.d(TAG, "Execution exception in display through Socket!");
		}
		XthLine = "You entered X = " + xvaluri + "\n" + XthLine;
		tw.setText(XthLine);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.display_xth_jsonhttp, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
